package com.zx.deploy.dao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.nio.charset.Charset;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONArray;
import com.zx.deploy.common.exception.BusinessException;
import com.zx.deploy.common.utils.DBUtil2;
import com.zx.deploy.common.utils.DatetimeUtil;
import com.zx.deploy.model.DatabaseConfig;

public class PermissionDao {
	Logger logger = Logger.getLogger(PermissionDao.class);

	public List<Map<String, Object>> getAllDataBase(DatabaseConfig config) throws Exception {
		String databaseName = config.databaseName;
		String sql = " select * from  information_schema.schemata  ";
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);
		List list2 = new ArrayList();
		Map map = new HashMap();
		map.put("SCHEMA_NAME", databaseName);
		list2.add(map);
		for (int i = 0; i < list.size(); ++i) {
			Map map2 = (Map) list.get(i);
			String schema_name = (String) map2.get("SCHEMA_NAME");
			if (!(schema_name.equals(databaseName))) {
				list2.add(map2);
			}
		}
		return list2;
	}

	public List<Map<String, Object>> getAllTables(DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql = " select TABLE_NAME from information_schema.TABLES where table_schema='" + config.databaseName
				+ "' and table_type='BASE TABLE' ";
		List list = db.queryForList(sql);
		return list;
	}

	public List<Map<String, Object>> getAllTables2(DatabaseConfig config, String tableName) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);

		String sql = " select * from information_schema.TABLES where table_schema='" + config.databaseName
				+ "' and table_type='BASE TABLE' ";

		if ("".equals(tableName))
			sql = " select TABLE_NAME,ENGINE, VERSION,ROW_FORMAT,TABLE_ROWS,AUTO_INCREMENT,TABLE_COLLATION,TABLE_COMMENT  from information_schema.TABLES where table_schema='"
					+ config.databaseName + "' and table_type='BASE TABLE' ";
		else {
			sql = " select TABLE_NAME,ENGINE, VERSION,ROW_FORMAT,TABLE_ROWS,AUTO_INCREMENT,TABLE_COLLATION,TABLE_COMMENT  from information_schema.TABLES where table_schema='"
					+ config.databaseName + "' and  table_name='" + tableName + "'  and table_type='BASE TABLE' ";
		}

		List list = db.queryForList(sql);
		return list;
	}

	public List<Map<String, Object>> getAllViews(DatabaseConfig config) throws Exception {
		String sql = " select TABLE_NAME   from information_schema.TABLES where table_schema='" + config.databaseName
				+ "' and table_type='VIEW' ";

		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);
		return list;
	}

	public List<Map<String, Object>> getAllFuntion(DatabaseConfig config) throws Exception {
		String sql = " select ROUTINE_NAME   from information_schema.ROUTINES where routine_schema='"
				+ config.databaseName + "' ";
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);
		return list;
	}

	public List<Map<String, Object>> getTableColumns(DatabaseConfig config, String tableName) throws Exception {
		String sql = "select * from  `" + config.databaseName + "`.`" + tableName + "` limit 1 ";
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForColumnOnly(sql);
		return list;
	}

	public List<Map<String, Object>> getTableColumns3(DatabaseConfig config, String tableName) throws Exception {
		String sql = " select column_name as TREESOFTPRIMARYKEY, COLUMN_NAME,COLUMN_TYPE , DATA_TYPE ,COLUMN_DEFAULT ,CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION ,NUMERIC_SCALE , IS_NULLABLE, COLUMN_KEY,extra, COLUMN_COMMENT  from information_schema.columns where   table_name='"
				+ tableName + "' and table_schema='" + config.databaseName + "'  ";
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);
		return list;
	}

	public List<Map<String, Object>> getTableIndexForMySQL(DatabaseConfig config, String tableName) throws Exception {
		String sql = " SELECT non_unique,index_name, column_name  from  information_schema.statistics where table_schema = '"
				+ config.databaseName + "' and  table_name ='" + tableName
				+ "'  and  index_name <>'PRIMARY'  order by index_name,seq_in_index;  ";

		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);

		return list;
	}


	public List<Map<String, Object>> selectAllDataFromSQLForMysql(DatabaseConfig config, String sql) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);
		return list;
	}

	public boolean checkSqlIsOneTableForMySql(DatabaseConfig config, String sql) {
		String temp = "";
		String tableName = "";
		try {
			DBUtil2 db = DBUtil2.getInstance(config);

			Pattern p = Pattern.compile("\\s+");
			Matcher m = p.matcher(sql);
			temp = m.replaceAll(" ");
			temp = temp.toLowerCase();

			if (temp.indexOf("select * from") >= 0) {
				for (int y = 14; y < temp.length(); ++y) {
					String c = String.valueOf(temp.charAt(y));
					if (c.equals(" "))
						break;
					tableName = tableName + c;
				}

				String sql2 = " select TABLE_NAME from information_schema.TABLES where table_schema='"
						+ config.databaseName + "' and table_type='BASE TABLE' and  TABLE_NAME ='" + tableName + "'";
				List list = db.queryForList(sql);

				if (list.size() > 0)
					return true;
			}
		} catch (Exception e) {
			return false;
		}
		return false;
	}

	public List<Map<String, Object>> executeSqlForColumns(String sql, DatabaseConfig config) throws Exception {
		String sql2 = " select * from  ( " + sql + " ) tab  limit 1 ";
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.executeSqlForColumns(sql2);
		return list;
	}

	public int executeSqlNotRes(String sql, DatabaseConfig config) throws Exception {
		Map map = new HashMap();
		DBUtil2 db = DBUtil2.getInstance(config);
		int i = db.setupdateData(sql);
		return i;
	}

	public int deleteRows(DatabaseConfig config, String tableName, String primary_key, String[] ids) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		int y = 0;
		for (int i = 0; i < ids.length; ++i) {
			String sql = " delete from  " + config.databaseName + "." + tableName + " where " + primary_key + " ='"
					+ ids[i] + "'";
			y += db.setupdateData(sql);
		}
		return y;
	}

	public int deleteRowsNew(DatabaseConfig config, String tableName, String primary_key, List<String> condition)
			throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		int y = 0;
		for (int i = 0; i < condition.size(); ++i) {
			String whereStr = (String) condition.get(i);

			String sql = " delete from  " + config.databaseName + "." + tableName + " where  1=1 " + whereStr;

			System.out.println("删除数据行= " + DatetimeUtil.getDefaultCurrentDateTime());
			System.out.println("SQL = " + sql);
			y += db.setupdateData(sql);
		}
		return y;
	}

	public int saveRows(Map<String, Object> map, String tableName, DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql = " insert into " + config.databaseName + "." + tableName;
		int y = 0;
		String colums = " ";
		String values = " ";
		for (Map.Entry entry : map.entrySet()) {
			colums = colums + ((String) entry.getKey()) + ",";

			Object str = entry.getValue();
			if (str.equals(""))
				values = values + " null ,";
			else {
				values = values + "'" + entry.getValue() + "',";
			}
		}

		colums = colums.substring(0, colums.length() - 1);
		values = values.substring(0, values.length() - 1);
		sql = sql + " (" + colums + ") values (" + values + ")";

		y = db.setupdateData(sql);
		return y;
	}

	public List<Map<String, Object>> getOneRowById(String tableName, String id, String idValues,
			DatabaseConfig config) {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql2 = " select * from   " + config.databaseName + "." + tableName + " where " + id + "='" + idValues
				+ "' ";
		List list = db.queryForListWithType(sql2);
		return list;
	}

	public int updateRows(Map<String, Object> map, String tableName, String id, String idValues, DatabaseConfig config)
			throws Exception {
		if ((id == null) || ("".equals(id))) {
			throw new Exception("数据不完整,保存失败!");
		}

		if ((idValues == null) || ("".equals(idValues))) {
			throw new Exception("数据不完整,保存失败!");
		}

		DBUtil2 db = DBUtil2.getInstance(config);
		String sql = " update  " + config.databaseName + "." + tableName;

		int y = 0;
		String ss = " set  ";

		for (Map.Entry entry : map.entrySet()) {
			String str = (String) entry.getValue();
			if (str.equals("")) {
				ss = ss + ((String) entry.getKey()) + "= null ,";
			} else if (entry.getValue() instanceof String)
				ss = ss + ((String) entry.getKey()) + "= '" + entry.getValue() + "',";
			else {
				ss = ss + ((String) entry.getKey()) + "= " + entry.getValue() + ",";
			}

		}

		ss = ss.substring(0, ss.length() - 1);
		sql = sql + ss + " where " + id + "='" + idValues + "'";

		this.logger.debug(DatetimeUtil.getDefaultCurrentDateTime() + ",SQL= " + sql);
		y = db.setupdateData(sql);
		return y;
	}

	public String getViewSql(String tableName, DatabaseConfig config) throws Exception {
		String sql = " select  view_definition  from  information_schema.VIEWS  where  table_name='" + tableName
				+ "' and table_schema='" + config.databaseName + "'  ";
		String str = "";
		DBUtil2 db = DBUtil2.getInstance(config);

		List list = db.queryForList(sql);

		if (list.size() == 1) {
			Map map = (Map) list.get(0);
			str = (String) map.get("view_definition");
		}
		return str;
	}

	public String getProcSqlForSQL(String proc_name, DatabaseConfig config) throws Exception {
		String sql = " show create procedure  " + proc_name;
		String str = "";
		DBUtil2 db = DBUtil2.getInstance(config);

		List list = db.queryForList(sql);

		if (list.size() == 1) {
			Map map = (Map) list.get(0);
			str = (String) map.get("Create Procedure");
		}
		return str;
	}

	public List<Map<String, Object>> getTableColumns2(String tableName, DatabaseConfig config) throws Exception {
		String sql = "select * from  `" + config.databaseName + "`.`" + tableName + "` limit 1";
		DBUtil2 db = DBUtil2.getInstance(config);

		List list = db.queryForColumnOnly(sql);

		return list;
	}

	public String getPrimaryKeys(String tableName, DatabaseConfig config) {
		DBUtil2 db = DBUtil2.getInstance(config);
		return db.getPrimaryKeys(config.databaseName, tableName);
	}

	public List<Map<String, Object>> getPrimaryKeyss(String tableName, DatabaseConfig config) throws Exception {
		String sql = " select   column_name  from information_schema.columns where   table_name='" + tableName
				+ "' and table_schema='" + config.databaseName + "' and column_key='PRI' ";

		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);

		return list;
	}

	public boolean testConn(DatabaseConfig config) {
		DBUtil2 db = DBUtil2.getInstance(config);
		return db.testConn();
	}

	public int saveDesginColumn(Map<String, String> map, String tableName, DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);

		String sql = " alter table " + config.databaseName + "." + tableName + " add column ";

		sql = sql + ((String) map.get("COLUMN_NAME")) + "  ";
		sql = sql + ((String) map.get("DATA_TYPE"));

		if ((map.get("CHARACTER_MAXIMUM_LENGTH") != null)
				&& (!(((String) map.get("CHARACTER_MAXIMUM_LENGTH")).equals("")))) {
			sql = sql + " (" + ((String) map.get("CHARACTER_MAXIMUM_LENGTH")) + ") ";
		}

		if ((map.get("COLUMN_COMMENT") != null) && (!(((String) map.get("COLUMN_COMMENT")).equals("")))) {
			sql = sql + " comment '" + ((String) map.get("COLUMN_COMMENT")) + "'";
		}

		int y = 0;

		y = db.setupdateData(sql);

		return y;
	}

	public int deleteTableColumn(String tableName, String[] ids, DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		int y = 0;
		for (int i = 0; i < ids.length; ++i) {
			String sql = " alter table   " + config.databaseName + "." + tableName + " drop column  " + ids[i];

			y += db.setupdateData(sql);
		}
		return y;
	}

	public int updateTableColumn(Map<String, Object> map, String tableName, String columnName, String idValues,
			DatabaseConfig config) throws Exception {
		if ((columnName == null) || ("".equals(columnName))) {
			throw new Exception("数据不完整,保存失败!");
		}

		if ((idValues == null) || ("".equals(idValues))) {
			throw new Exception("数据不完整,保存失败!");
		}

		DBUtil2 db = DBUtil2.getInstance(config);

		String old_field_name = (String) map.get("TREESOFTPRIMARYKEY");
		String column_name = (String) map.get("COLUMN_NAME");
		String data_type = (String) map.get("DATA_TYPE");
		String character_maximum_length = (String) map.get("CHARACTER_MAXIMUM_LENGTH");
		String column_comment = (String) map.get("COLUMN_COMMENT");

		if (!(old_field_name.endsWith(column_name))) {
			String sql = " alter table  " + config.databaseName + "." + tableName + " change ";
			sql = sql + old_field_name + " " + column_name + " " + data_type;
			if ((character_maximum_length != null) && (!(character_maximum_length.equals("")))) {
				sql = sql + " (" + character_maximum_length + ")";
			}

			int i = db.setupdateData(sql);
		}

		String sql2 = " alter table  " + config.databaseName + "." + tableName + " modify column " + column_name + " "
				+ data_type;

		if ((character_maximum_length != null) && (!(character_maximum_length.equals("")))) {
			sql2 = sql2 + " (" + character_maximum_length + ")";
		}

		if ((column_comment != null) && (!(column_comment.equals("")))) {
			sql2 = sql2 + " comment '" + column_comment + "'";
		}

		int y = db.setupdateData(sql2);

		return y;
	}

	public int dropPrimaryKey(String tableName, DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql4 = " alter table  " + config.databaseName + "." + tableName + " drop primary key ";

		db.setupdateData(sql4);

		return 0;
	}

	public int savePrimaryKey2(String tableName, String primaryKeys, DatabaseConfig config) throws Exception {
		String sql4 = "";
		if ((primaryKeys != null) && (!(primaryKeys.equals("")))) {
			DBUtil2 db = DBUtil2.getInstance(config);
			sql4 = " alter table  " + config.databaseName + "." + tableName + " add primary key (" + primaryKeys + ")";

			db.setupdateData(sql4);
		}
		return 0;
	}

	public int savePrimaryKey(String tableName, String column_name, String isSetting, DatabaseConfig config)
			throws Exception {
		String sql4 = "";
		if ((column_name != null) && (!(column_name.equals("")))) {
			DBUtil2 db = DBUtil2.getInstance(config);

			List list2 = selectTablePrimaryKey(tableName, config);
			if (isSetting.equals("true"))
				list2.add(column_name);
			else {
				list2.remove(column_name);
			}

			String tem = list2.toString();
			String primaryKey = tem.substring(1, tem.length() - 1);

			if (primaryKey.equals(""))
				sql4 = " alter table  " + config.databaseName + "." + tableName + " drop primary key ";
			else if ((list2.size() == 1) && (isSetting.equals("true")))
				sql4 = " alter table  " + config.databaseName + "." + tableName + " add primary key (" + primaryKey
						+ ")";
			else {
				sql4 = " alter table  " + config.databaseName + "." + tableName + " drop primary key, add primary key ("
						+ primaryKey + ")";
			}
			db.setupdateData(sql4);
		}

		return 0;
	}

	public List<String> selectTablePrimaryKey(String tableName, DatabaseConfig config) throws Exception {
		String sql = " select column_name   from information_schema.columns where   table_name='" + tableName
				+ "' and table_schema='" + config.databaseName + "'  and column_key='PRI' ";

		DBUtil2 db = DBUtil2.getInstance(config);
		List<Map<String, Object>> list = db.queryForList(sql);

		List list2 = new ArrayList();

		for (Map map : list) {
			list2.add((String) map.get("column_name"));
		}

		return list2;
	}

	public String selectOneColumnType(String tableName, String column_name, DatabaseConfig config) throws Exception {
		String sql = " select   column_type  from information_schema.columns where   table_name='" + tableName
				+ "' and table_schema='" + config.databaseName + "' and column_name='" + column_name + "'";

		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);
		return ((String) ((Map) list.get(0)).get("column_type"));
	}

	public int updateTableNullAble(String tableName, String column_name, String is_nullable, DatabaseConfig config)
			throws Exception {
		String sql4 = "";
		if ((column_name != null) && (!(column_name.equals("")))) {
			DBUtil2 db = DBUtil2.getInstance(config);
			String column_type = selectOneColumnType(tableName, column_name, config);

			if (is_nullable.equals("true"))
				sql4 = " alter table  " + config.databaseName + "." + tableName + " modify column " + column_name + " "
						+ column_type + "  null ";
			else {
				sql4 = " alter table  " + config.databaseName + "." + tableName + " modify column " + column_name + " "
						+ column_type + " not null ";
			}
			db.setupdateData(sql4);
		}
		return 0;
	}

	public int upDownColumn(String tableName, String column_name, String column_name2, DatabaseConfig config)
			throws Exception {
		String sql4 = "";
		if ((column_name != null) && (!(column_name.equals("")))) {
			DBUtil2 db = DBUtil2.getInstance(config);
			String column_type = selectOneColumnType(tableName, column_name, config);
			if ((column_name2 == null) || (column_name2.equals("")))
				sql4 = " alter table  " + config.databaseName + "." + tableName + " modify column " + column_name + " "
						+ column_type + " first ";
			else {
				sql4 = " alter table  " + config.databaseName + "." + tableName + " modify column " + column_name + " "
						+ column_type + " after " + column_name2;
			}

			db.setupdateData(sql4);
		}
		return 0;
	}

	public List<Map<String, Object>> getAllDataBaseForOracle(DatabaseConfig config) throws Exception {
		List list = new ArrayList();
		Map map = new HashMap();
		map.put("SCHEMA_NAME", config.databaseName);
		list.add(map);
		return list;
	}

	public List<Map<String, Object>> getAllTablesForOracle( DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);

		String sql = " select TABLE_NAME  from  user_tables  ";

		List list = db.queryForList(sql);

		return list;
	}

	public List<Map<String, Object>> getTableColumns3ForOracle( String tableName, DatabaseConfig config)
			throws Exception {
		String sql = "select t1.column_name as TREESOFTPRIMARYKEY, t1.COLUMN_NAME,  nvl2( t1.CHAR_COL_DECL_LENGTH,  t1.DATA_TYPE||'(' ||CHAR_COL_DECL_LENGTH||')',t1.DATA_TYPE ) as COLUMN_TYPE ,t1.data_type,   t1.data_length as CHARACTER_MAXIMUM_LENGTH ,CASE t1.nullable when 'Y' then 'YES' END as IS_NULLABLE  ,  nvl2(t3.column_name ,'PRI' ,'')  as COLUMN_KEY,  t2.comments as COLUMN_COMMENT  from user_tab_columns  t1   left join user_col_comments t2  on  t1.table_name = t2.table_name and t1.COLUMN_NAME = t2.COLUMN_NAME   left join   (select a.table_name, a.column_name   from user_cons_columns a, user_constraints b    where a.constraint_name = b.constraint_name    and b.constraint_type = 'P' ) t3    on t1.TABLE_NAME = t3.table_name  and t1.COLUMN_NAME = t3.COLUMN_NAME    where   t1.table_name= '"
				+ tableName + "'  ";

		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);

		return list;
	}

	public List<Map<String, Object>> getAllViewsForOracle( DatabaseConfig config) throws Exception {
		String sql = " select view_name as TABLE_NAME from  user_views  ";

		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);
		return list;
	}

	public String getViewSqlForOracle(String tableName, DatabaseConfig config) throws Exception {
		String sql = " select  view_definition  from  information_schema.VIEWS  where  table_name='" + tableName
				+ "' and table_schema='" + config.databaseName + "'  ";
		String str = "";
		DBUtil2 db = DBUtil2.getInstance(config);

		List list = db.queryForList(sql);

		if (list.size() == 1) {
			Map map = (Map) list.get(0);
			str = (String) map.get("view_definition");
		}
		return str;
	}

	public List<Map<String, Object>> getAllFuntionForOracle( DatabaseConfig config) throws Exception {
		String sql = " select object_name as ROUTINE_NAME from  user_procedures ";
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);
		return list;
	}

	public List<Map<String, Object>> getPrimaryKeyssForOracle(String tableName, DatabaseConfig config)
			throws Exception {
		String sql = "  select  COLUMN_NAME   from   user_cons_columns  where   constraint_name= (select  constraint_name  from user_constraints  where table_name = '"
				+ tableName + "' and constraint_type ='P') ";
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);

		return list;
	}

	public List<Map<String, Object>> getTableColumnsForOracle( String tableName, DatabaseConfig config)
			throws Exception {
		String sql = "select  * from   " + tableName + " where rownum =1 ";
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForColumnOnly(sql);

		return list;
	}

	public List<Map<String, Object>> executeSqlForColumnsForOracle(String sql,  DatabaseConfig config)
			throws Exception {
		String sql2 = " select * from (" + sql + ") where  rownum = 1 ";
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.executeSqlForColumns(sql2);
		return list;
	}

	public int updateTableNullAbleForOracle(String tableName, String column_name, String is_nullable,
			DatabaseConfig config) throws Exception {
		String sql4 = "";
		if ((column_name != null) && (!(column_name.equals("")))) {
			DBUtil2 db = DBUtil2.getInstance(config);

			if (is_nullable.equals("true"))
				sql4 = " alter table  " + tableName + " modify   " + column_name + "  null ";
			else {
				sql4 = " alter table  " + tableName + " modify   " + column_name + "  not null ";
			}
			db.setupdateData(sql4);
		}
		return 0;
	}

	public int savePrimaryKeyForOracle(String tableName, String column_name, String isSetting, DatabaseConfig config)
			throws Exception {
		String sql4 = "";
		if ((column_name != null) && (!(column_name.equals("")))) {
			DBUtil2 db = DBUtil2.getInstance(config);

			List<Map<String, Object>> list2 = selectTablePrimaryKeyForOracle(tableName, config);

			List list3 = new ArrayList();
			for (Map map : list2) {
				list3.add((String) map.get("COLUMN_NAME"));
			}

			if (isSetting.equals("true"))
				list3.add(column_name);
			else {
				list3.remove(column_name);
			}

			String tem = list3.toString();
			String primaryKey = tem.substring(1, tem.length() - 1);

			if (list2.size() > 0) {
				String temp = (String) ((Map) list2.get(0)).get("CONSTRAINT_NAME");
				sql4 = " alter table   " + tableName + " drop constraint  " + temp;
				db.setupdateData(sql4);
			}

			if (!(primaryKey.equals(""))) {
				sql4 = " alter table " + tableName + " add   primary key (" + primaryKey + ") ";
				db.setupdateData(sql4);
			}
		}
		return 0;
	}

	public List<Map<String, Object>> selectTablePrimaryKeyForOracle(String tableName, DatabaseConfig config)
			throws Exception {
		String sql = " select a.CONSTRAINT_NAME,  a.COLUMN_NAME  from user_cons_columns a, user_constraints b  where a.constraint_name = b.constraint_name   and b.constraint_type = 'P'  and a.table_name = '"
				+ tableName + "' ";

		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);

		List list2 = new ArrayList();

		return list;
	}

	public int saveDesginColumnForOracle(Map<String, String> map, String tableName, DatabaseConfig config)
			throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql = " alter table " + tableName + " add  ";
		sql = sql + ((String) map.get("COLUMN_NAME")) + "  ";
		sql = sql + ((String) map.get("DATA_TYPE"));

		if ((map.get("CHARACTER_MAXIMUM_LENGTH") != null)
				&& (!(((String) map.get("CHARACTER_MAXIMUM_LENGTH")).equals("")))) {
			sql = sql + " (" + ((String) map.get("CHARACTER_MAXIMUM_LENGTH")) + ") ";
		}

		if ((map.get("COLUMN_COMMENT") != null) && (!(((String) map.get("COLUMN_COMMENT")).equals("")))) {
			sql = sql + " comment '" + ((String) map.get("COLUMN_COMMENT")) + "'";
		}
		int y = 0;

		y = db.setupdateData(sql);
		return y;
	}

	public int updateTableColumnForOracle(Map<String, Object> map, String tableName, String columnName, String idValues,
			DatabaseConfig config) throws Exception {
		if ((columnName == null) || ("".equals(columnName))) {
			throw new Exception("数据不完整,保存失败!");
		}

		if ((idValues == null) || ("".equals(idValues))) {
			throw new Exception("数据不完整,保存失败!");
		}

		DBUtil2 db = DBUtil2.getInstance(config);
		String old_field_name = (String) map.get("TREESOFTPRIMARYKEY");
		String column_name = (String) map.get("COLUMN_NAME");
		String data_type = (String) map.get("DATA_TYPE");
		String character_maximum_length = (String) map.get("CHARACTER_MAXIMUM_LENGTH");
		String column_comment = (String) map.get("COLUMN_COMMENT");

		if (!(old_field_name.endsWith(column_name))) {
			String sql = " ALTER TABLE " + tableName + " RENAME COLUMN " + old_field_name + " to  " + column_name;

			int i = db.setupdateData(sql);
		}

		String sql2 = " alter table  " + tableName + " modify  " + column_name + " " + data_type;
		if ((character_maximum_length != null) && (!(character_maximum_length.equals("")))) {
			sql2 = sql2 + " (" + character_maximum_length + ")";
		}
		int y = db.setupdateData(sql2);

		if ((column_comment != null) && (!(column_comment.equals("")))) {
			String sql4 = "  comment on column " + tableName + "." + column_name + " is '" + column_comment + "' ";
			db.setupdateData(sql4);
		}
		return y;
	}

	public int deleteTableColumnForOracle(String tableName, String[] ids, DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		int y = 0;
		for (int i = 0; i < ids.length; ++i) {
			String sql = " alter table   " + tableName + " drop (" + ids[i] + ")";

			y += db.setupdateData(sql);
		}
		return y;
	}

	public int saveRowsForOracle(Map<String, String> map, String tableName, DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql = " insert into  " + tableName;
		int y = 0;
		String colums = " ";
		String values = " ";
		String columnType = "";
		for (Map.Entry entry : map.entrySet()) {
			if (((String) entry.getKey()).equals("RN")) {
				continue;
			}
			colums = colums + ((String) entry.getKey()) + ",";

			columnType = selectColumnTypeForOracle(tableName, (String) entry.getKey(), config);

			String str = (String) entry.getValue();
			if (str.equals("")) {
				values = values + " null ,";
			} else if (columnType.equals("DATE")) {
				values = values + " to_date('" + ((String) entry.getValue()) + "' ,'yyyy-mm-dd hh24:mi:ss') ,";
			} else if (columnType.equals("integer"))
				values = values + ((String) entry.getValue()) + ",";
			else {
				values = values + "'" + ((String) entry.getValue()) + "',";
			}

		}

		colums = colums.substring(0, colums.length() - 1);
		values = values.substring(0, values.length() - 1);
		sql = sql + " (" + colums + ") values (" + values + ")";
		System.out.println("sql=" + sql);
		y = db.setupdateData(sql);
		return y;
	}

	public String selectColumnTypeForOracle(String tableName, String column, DatabaseConfig config) throws Exception {
		String sql = " select DATA_TYPE from user_tab_columns where table_name ='" + tableName + "' AND COLUMN_NAME ='"
				+ column + "' ";

		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);
		return ((String) ((Map) list.get(0)).get("DATA_TYPE"));
	}

	public int deleteRowsNewForOracle(String tableName, String primary_key, List<String> condition,
			DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		int y = 0;
		for (int i = 0; i < condition.size(); ++i) {
			String whereStr = (String) condition.get(i);

			String sql = " delete from  " + tableName + " where  1=1 " + whereStr;

			java.util.Date d = new java.util.Date();

			this.logger.debug(DatetimeUtil.getDefaultCurrentDateTime() + ",SQL= " + sql);
			System.out.println("删除数据行= " + DatetimeUtil.getDefaultCurrentDateTime());
			System.out.println("SQL = " + sql);
			y += db.setupdateData(sql);
		}
		return y;
	}

	public List<Map<String, Object>> getAllDataBaseForPostgreSQL(DatabaseConfig config) throws Exception {
		List list = new ArrayList();
		Map map = new HashMap();
		map.put("SCHEMA_NAME", config.databaseName);
		list.add(map);
		return list;
	}

	public List<Map<String, Object>> getAllTablesForPostgreSQL(DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);

		String sql = " select  tablename as \"TABLE_NAME\" from pg_tables  where schemaname='public'  ";

		List list = db.queryForList(sql);
		return list;
	}

	public List<Map<String, Object>> getTableColumns3ForPostgreSQL(String tableName, DatabaseConfig config)
			throws Exception {
		String sql = "   select t1.column_name as \"TREESOFTPRIMARYKEY\", t1.COLUMN_NAME as \"COLUMN_NAME\", t1.DATA_TYPE   as \"COLUMN_TYPE\" , t1.DATA_TYPE as \"DATA_TYPE\" , character_maximum_length as \"CHARACTER_MAXIMUM_LENGTH\" ,   t1.IS_NULLABLE as \"IS_NULLABLE\" ,  '' as \"COLUMN_COMMENT\" , CASE  WHEN t2.COLUMN_NAME IS NULL THEN ''  ELSE 'PRI'  END AS \"COLUMN_KEY\"   from information_schema.columns t1    left join information_schema.constraint_column_usage t2    on t1.table_name = t2.table_name  and t1.COLUMN_NAME = t2.COLUMN_NAME where  t1.table_name='"
				+ tableName + "'    ";
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);

		return list;
	}

	public List<Map<String, Object>> getAllViewsForPostgreSQL(DatabaseConfig config) throws Exception {
		String sql = " select   viewname as \"TABLE_NAME\"  from pg_views  where schemaname='public'  ";

		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);
		return list;
	}

	public List<Map<String, Object>> getAllFuntionForPostgreSQL(DatabaseConfig config) throws Exception {
		String sql = "  select prosrc as \"ROUTINE_NAME\" from pg_proc where 1=2  ";

		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);
		return list;
	}

	public int deleteRowsNewForPostgreSQL(String tableName, String primary_key, List<String> condition,
			DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		int y = 0;
		for (int i = 0; i < condition.size(); ++i) {
			String whereStr = (String) condition.get(i);

			String sql = " delete from  " + tableName + " where  1=1 " + whereStr;

			System.out.println("删除数据行= " + DatetimeUtil.getDefaultCurrentDateTime());
			System.out.println("SQL = " + sql);
			y += db.setupdateData(sql);
		}
		return y;
	}

	public int saveRowsForPostgreSQL(Map<String, String> map, String tableName, DatabaseConfig config)
			throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql = " insert into  " + tableName;
		int y = 0;
		String colums = " ";
		String values = " ";
		String columnType = "";
		for (Map.Entry entry : map.entrySet()) {
			colums = colums + ((String) entry.getKey()) + ",";

			columnType = selectColumnTypeForPostgreSQL(tableName, (String) entry.getKey(), config);

			String str = (String) entry.getValue();
			if (str.equals("")) {
				values = values + " null ,";
			} else if (columnType.equals("integer"))
				values = values + ((String) entry.getValue()) + " ,";
			else if (columnType.equals("numeric"))
				values = values + ((String) entry.getValue()) + " ,";
			else if (columnType.equals("real"))
				values = values + ((String) entry.getValue()) + " ,";
			else {
				values = values + "'" + ((String) entry.getValue()) + "',";
			}

		}

		colums = colums.substring(0, colums.length() - 1);
		values = values.substring(0, values.length() - 1);
		sql = sql + " (" + colums + ") values (" + values + ")";

		y = db.setupdateData(sql);
		return y;
	}

	public int saveDesginColumnForPostgreSQL(Map<String, String> map, String tableName, DatabaseConfig config)
			throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);

		String sql = " alter table " + tableName + " add  ";

		sql = sql + ((String) map.get("COLUMN_NAME")) + "  ";
		sql = sql + ((String) map.get("DATA_TYPE"));

		if ((map.get("CHARACTER_MAXIMUM_LENGTH") != null)
				&& (!(((String) map.get("CHARACTER_MAXIMUM_LENGTH")).equals("")))) {
			sql = sql + " (" + ((String) map.get("CHARACTER_MAXIMUM_LENGTH")) + ") ";
		}

		if ((map.get("COLUMN_COMMENT") != null) && (!(((String) map.get("COLUMN_COMMENT")).equals("")))) {
			sql = sql + " comment '" + ((String) map.get("COLUMN_COMMENT")) + "'";
		}
		int y = 0;

		y = db.setupdateData(sql);
		return y;
	}

	public int updateTableColumnForPostgreSQL(Map<String, Object> map, String tableName, String columnName,
			String idValues, DatabaseConfig config) throws Exception {
		if ((columnName == null) || ("".equals(columnName))) {
			throw new Exception("数据不完整,保存失败!");
		}

		if ((idValues == null) || ("".equals(idValues))) {
			throw new Exception("数据不完整,保存失败!");
		}

		DBUtil2 db = DBUtil2.getInstance(config);

		String old_field_name = (String) map.get("TREESOFTPRIMARYKEY");
		String column_name = (String) map.get("COLUMN_NAME");
		String data_type = (String) map.get("DATA_TYPE");
		String character_maximum_length = (String) map.get("CHARACTER_MAXIMUM_LENGTH");
		String column_comment = (String) map.get("COLUMN_COMMENT");

		if (!(old_field_name.endsWith(column_name))) {
			String sql = " ALTER TABLE " + tableName + " RENAME COLUMN " + old_field_name + " to  " + column_name;
			System.out.println(sql);
			int i = db.setupdateData(sql);
		}

		String sql2 = " alter table  " + tableName + " alter column  " + column_name + " type " + data_type;
		if ((character_maximum_length != null) && (!(character_maximum_length.equals("")))) {
			sql2 = sql2 + " (" + character_maximum_length + ")";
		}
		System.out.println(sql2);
		int y = db.setupdateData(sql2);

		if ((column_comment != null) && (!(column_comment.equals("")))) {
			String sql4 = "  comment on column " + tableName + "." + column_name + " is '" + column_comment + "' ";
			db.setupdateData(sql4);
		}

		return y;
	}

	public int deleteTableColumnForPostgreSQL(String tableName, String[] ids, DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		int y = 0;
		for (int i = 0; i < ids.length; ++i) {
			String sql = " alter table   " + tableName + " drop (" + ids[i] + ")";

			y += db.setupdateData(sql);
		}
		return y;
	}

	public int updateTableNullAbleForPostgreSQL(String tableName, String column_name, String is_nullable,
			DatabaseConfig config) throws Exception {
		String sql4 = "";
		if ((column_name != null) && (!(column_name.equals("")))) {
			DBUtil2 db = DBUtil2.getInstance(config);

			if (is_nullable.equals("true"))
				sql4 = " alter table  " + tableName + " alter column   " + column_name + " drop not null ";
			else {
				sql4 = " alter table  " + tableName + " alter column   " + column_name + " set not null ";
			}
			db.setupdateData(sql4);
		}
		return 0;
	}

	public int savePrimaryKeyForPostgreSQL(String tableName, String column_name, String isSetting,
			DatabaseConfig config) throws Exception {
		String sql4 = "";
		if ((column_name != null) && (!(column_name.equals("")))) {
			DBUtil2 db = DBUtil2.getInstance(config);

			List<Map<String, Object>> list2 = selectTablePrimaryKeyForPostgreSQL(tableName, config);

			List list3 = new ArrayList();
			for (Map map : list2) {
				list3.add((String) map.get("COLUMN_NAME"));
			}

			if (isSetting.equals("true"))
				list3.add(column_name);
			else {
				list3.remove(column_name);
			}

			String tem = list3.toString();
			String primaryKey = tem.substring(1, tem.length() - 1);

			if (list2.size() > 0) {
				String temp = (String) ((Map) list2.get(0)).get("CONSTRAINT_NAME");
				sql4 = " alter table   " + tableName + " drop constraint  " + temp;
				db.setupdateData(sql4);
			}

			if (!(primaryKey.equals(""))) {
				sql4 = " alter table " + tableName + " add   primary key (" + primaryKey + ") ";
				db.setupdateData(sql4);
			}
		}
		return 0;
	}

	public List<Map<String, Object>> getPrimaryKeyssForPostgreSQL(String tableName, DatabaseConfig config)
			throws Exception {
		String sql = " select  pg_attribute.attname as \"COLUMN_NAME\" from   pg_constraint  inner join pg_class    on pg_constraint.conrelid = pg_class.oid    inner join pg_attribute on pg_attribute.attrelid = pg_class.oid    and  pg_attribute.attnum = pg_constraint.conkey[1]     inner join pg_type on pg_type.oid = pg_attribute.atttypid  where pg_class.relname = '"
				+ tableName + "'  " + " and pg_constraint.contype='p' ";

		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);

		return list;
	}

	public List<Map<String, Object>> getTableColumnsForPostgreSQL(String tableName, DatabaseConfig config)
			throws Exception {
		String sql = "select  * from   " + tableName + " limit 1 ";
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForColumnOnly(sql);

		return list;
	}

	public String selectColumnTypeForPostgreSQL(String tableName, String column, DatabaseConfig config)
			throws Exception {
		String sql = " select data_type as \"DATA_TYPE\"  from  information_schema.columns  where    table_name ='"
				+ tableName + "' AND COLUMN_NAME ='" + column + "' ";

		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);
		return ((String) ((Map) list.get(0)).get("DATA_TYPE"));
	}

	public List<Map<String, Object>> selectTablePrimaryKeyForPostgreSQL(String tableName, DatabaseConfig config)
			throws Exception {
		String sql = " select pg_constraint.conname as \"CONSTRAINT_NAME\" ,pg_attribute.attname as \"COLUMN_NAME\" ,pg_type.typname as typename from   pg_constraint  inner join pg_class   on pg_constraint.conrelid = pg_class.oid    inner join pg_attribute on pg_attribute.attrelid = pg_class.oid    and  pg_attribute.attnum = pg_constraint.conkey[1]   inner join pg_type on pg_type.oid = pg_attribute.atttypid    where pg_class.relname = '"
				+ tableName + "'  " + "  and pg_constraint.contype='p' ";

		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);

		List list2 = new ArrayList();

		return list;
	}

	public List<Map<String, Object>> executeSqlForColumnsForPostgreSQL(String sql, DatabaseConfig config)
			throws Exception {
		String sql2 = " select * from (" + sql + ") t   limit 1; ";
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.executeSqlForColumns(sql2);
		return list;
	}

	public String getViewSqlForPostgreSQL(String tableName, DatabaseConfig config) throws Exception {
		String sql = " select  view_definition  from  information_schema.views  where  table_name='" + tableName
				+ "' and table_catalog='" + config.databaseName + "'  ";
		String str = " ";
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);
		if (list.size() == 1) {
			Map map = (Map) list.get(0);
			str = (String) map.get("view_definition");
		}
		return str;
	}

	public List<Map<String, Object>> getAllDataBaseForMSSQL(DatabaseConfig config) throws Exception {
		String sql = " SELECT name as SCHEMA_NAME FROM sys.databases where state='0' ORDER BY name  ";

		List list = new ArrayList();
		try {
			DBUtil2 db = DBUtil2.getInstance(config);
			list = db.queryForList(sql);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		List list2 = new ArrayList();
		Map map = new HashMap();
		map.put("SCHEMA_NAME", config.databaseName);
		list2.add(map);
		for (int i = 0; i < list.size(); ++i) {
			Map map2 = (Map) list.get(i);
			String schema_name = (String) map2.get("SCHEMA_NAME");

			if (!(schema_name.equals(config.databaseName))) {
				list2.add(map2);
			}
		}
		return list2;
	}

	public List<Map<String, Object>> getAllTablesForMSSQL(DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql = " SELECT Name as TABLE_NAME FROM " + config.databaseName
				+ "..SysObjects Where XType='U' ORDER BY Name ";
		List list = db.queryForList(sql);
		return list;
	}

	public List<Map<String, Object>> getTableColumns3ForMSSQL(String tableName, DatabaseConfig config)
			throws Exception {
		String sql = " select b.name   TREESOFTPRIMARYKEY, b.name COLUMN_NAME, ISNULL( c.name +'('+  cast(b.length as varchar(10)) +')' , c.name ) as  COLUMN_TYPE, c.name DATA_TYPE, b.length CHARACTER_MAXIMUM_LENGTH ,  case when b.isnullable=1  then 'YES' else 'NO' end as IS_NULLABLE , '' as COLUMN_COMMENT , (SELECT 'PRI' FROM sysobjects where xtype='PK' and  parent_obj=b.id and name in (    SELECT name  FROM sysindexes   WHERE indid in(    SELECT indid FROM sysindexkeys WHERE id = b.id AND colid=b.colid  ))) as COLUMN_KEY  from sysobjects a,syscolumns b,systypes c  where a.id=b.id  and a.name='"
				+ tableName + "' and a.xtype='U'  and b.xtype=c.xtype and c.name<>'sysname' ";

		DBUtil2 db = DBUtil2.getInstance(config);
		List<Map<String, Object>> list = db.queryForList2(sql);

		List tempList = new ArrayList();
		for (Map mmap : list) {
			String data_type = (String) mmap.get("DATA_TYPE");
			if (data_type.equals("nvarchar")) {
				int leng = ((Short) mmap.get("CHARACTER_MAXIMUM_LENGTH")).shortValue();
				mmap.put("CHARACTER_MAXIMUM_LENGTH", Integer.valueOf(leng / 2));
			}
			tempList.add(mmap);
		}

		return tempList;
	}

	public List<Map<String, Object>> getAllViewsForMSSQL(DatabaseConfig config) throws Exception {
		String sql = "  SELECT  NAME AS TABLE_NAME FROM  sysobjects where XTYPE ='V'  ";

		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);
		return list;
	}

	public List<Map<String, Object>> getAllFuntionForMSSQL(DatabaseConfig config) throws Exception {
		String sql = " SELECT  NAME AS ROUTINE_NAME FROM  sysobjects where XTYPE ='FN' ";

		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);
		return list;
	}

	public int deleteRowsNewForMSSQL(String tableName, String primary_key, List<String> condition,
			DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		int y = 0;
		for (int i = 0; i < condition.size(); ++i) {
			String whereStr = (String) condition.get(i);

			String sql = " delete from  " + tableName + " where  1=1 " + whereStr;

			System.out.println("删除数据行= " + DatetimeUtil.getDefaultCurrentDateTime());
			System.out.println("SQL = " + sql);
			y += db.setupdateData(sql);
		}
		return y;
	}

	public String getViewSqlForMSSQL(String tableName, DatabaseConfig config) throws Exception {
		String sql = " select  view_definition  from  information_schema.views  where  table_name='" + tableName
				+ "' and table_catalog='" + config.databaseName + "'  ";
		String str = " ";
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);
		if (list.size() == 1) {
			Map map = (Map) list.get(0);
			str = (String) map.get("view_definition");
		}
		return str;
	}

	public int saveRowsForMSSQL(Map<String, String> map, String tableName, DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql = " insert into  " + tableName;
		int y = 0;
		String colums = " ";
		String values = " ";
		String columnType = "";
		for (Map.Entry entry : map.entrySet()) {
			colums = colums + ((String) entry.getKey()) + ",";
			columnType = selectColumnTypeForPostgreSQL(tableName, (String) entry.getKey(), config);
			String str = (String) entry.getValue();
			if (str.equals("")) {
				values = values + " null ,";
			} else if (columnType.equals("DATE"))
				values = values + " to_date('" + ((String) entry.getValue()) + "' ,'yyyy-mm-dd hh24:mi:ss') ,";
			else {
				values = values + "'" + ((String) entry.getValue()) + "',";
			}

		}

		colums = colums.substring(0, colums.length() - 1);
		values = values.substring(0, values.length() - 1);
		sql = sql + " (" + colums + ") values (" + values + ")";
		y = db.setupdateData(sql);
		return y;
	}

	public int saveDesginColumnForMSSQL(Map<String, String> map, String tableName, DatabaseConfig config)
			throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);

		String sql = " alter table " + tableName + " add  ";

		sql = sql + ((String) map.get("COLUMN_NAME")) + "  ";
		sql = sql + ((String) map.get("DATA_TYPE"));

		if ((map.get("CHARACTER_MAXIMUM_LENGTH") != null)
				&& (!(((String) map.get("CHARACTER_MAXIMUM_LENGTH")).equals("")))) {
			sql = sql + " (" + ((String) map.get("CHARACTER_MAXIMUM_LENGTH")) + ") ";
		}

		if ((map.get("COLUMN_COMMENT") != null) && (!(((String) map.get("COLUMN_COMMENT")).equals("")))) {
			sql = sql + " comment '" + ((String) map.get("COLUMN_COMMENT")) + "'";
		}
		int y = 0;

		y = db.setupdateData(sql);
		return y;
	}

	public int updateTableColumnForMSSQL(Map<String, Object> map, String tableName, String columnName, String idValues,
			DatabaseConfig config) throws Exception {
		if ((columnName == null) || ("".equals(columnName))) {
			throw new Exception("数据不完整,保存失败!");
		}

		if ((idValues == null) || ("".equals(idValues))) {
			throw new Exception("数据不完整,保存失败!");
		}

		DBUtil2 db = DBUtil2.getInstance(config);

		String old_field_name = (String) map.get("TREESOFTPRIMARYKEY");
		String column_name = (String) map.get("COLUMN_NAME");
		String data_type = (String) map.get("DATA_TYPE");
		String character_maximum_length = (String) map.get("CHARACTER_MAXIMUM_LENGTH");
		String column_comment = (String) map.get("COLUMN_COMMENT");

		if (!(old_field_name.endsWith(column_name))) {
			String sql = " exec sp_rename '" + tableName + "." + old_field_name + "','" + column_name + "','COLUMN'";

			int i = db.setupdateData(sql);
		}

		String sql2 = " alter table  " + tableName + " alter column " + column_name + " " + data_type;

		if ((!(data_type.equals("int"))) && (!(data_type.equals("date"))) && (character_maximum_length != null)
				&& (!(character_maximum_length.equals("")))) {
			sql2 = sql2 + " (" + character_maximum_length + ")";
		}

		int y = db.setupdateData(sql2);

		if ((column_comment != null) && (!(column_comment.equals("")))) {
			String sql4 = "exec sp_addextendedproperty 'MS_Description','" + column_comment + "','user','dbo','TABLE','"
					+ tableName + "','column','" + column_name + "' ";
			System.out.println(sql4);
			db.setupdateData(sql4);
		}

		return y;
	}

	public int deleteTableColumnForMSSQL(String tableName, String[] ids, DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		int y = 0;
		for (int i = 0; i < ids.length; ++i) {
			String sql = " alter table   " + tableName + " drop (" + ids[i] + ")";

			y += db.setupdateData(sql);
		}
		return y;
	}

	public int updateTableNullAbleForMSSQL(String tableName, String column_name, String is_nullable,
			DatabaseConfig config) throws Exception {
		String sql4 = "";
		if ((column_name != null) && (!(column_name.equals("")))) {
			DBUtil2 db = DBUtil2.getInstance(config);

			String column_type = selectOneColumnTypeForMSSQL(tableName, column_name, config);

			if (is_nullable.equals("true"))
				sql4 = " alter table  " + tableName + " alter column   " + column_name + " " + column_type + " "
						+ "  null ";
			else {
				sql4 = " alter table  " + tableName + " alter column   " + column_name + " " + column_type + " "
						+ "  not null ";
			}
			db.setupdateData(sql4);
		}
		return 0;
	}

	public int savePrimaryKeyForMSSQL(String tableName, String column_name, String isSetting, DatabaseConfig config)
			throws Exception {
		String sql4 = "";
		if ((column_name != null) && (!(column_name.equals("")))) {
			DBUtil2 db = DBUtil2.getInstance(config);

			List<Map<String, Object>> list2 = selectTablePrimaryKeyForMSSQL(tableName, config);

			List list3 = new ArrayList();
			for (Map map : list2) {
				list3.add((String) map.get("COLUMN_NAME"));
			}

			if (isSetting.equals("true"))
				list3.add(column_name);
			else {
				list3.remove(column_name);
			}

			String tem = list3.toString();
			String primaryKey = tem.substring(1, tem.length() - 1);

			if (list2.size() > 0) {
				String temp = (String) ((Map) list2.get(0)).get("CONSTRAINT_NAME");
				sql4 = " alter table   " + tableName + " drop constraint  " + temp;
				System.out.println("1=" + sql4);
				db.setupdateData(sql4);
			}

			if (!(primaryKey.equals(""))) {
				sql4 = " alter table " + tableName + " add   primary key (" + primaryKey + ") ";
				System.out.println("2=" + sql4);
				db.setupdateData(sql4);
			}
		}
		return 0;
	}

	public List<Map<String, Object>> selectTablePrimaryKeyForMSSQL(String tableName, DatabaseConfig config)
			throws Exception {
		String sql = "  select  b.CONSTRAINT_NAME, b.COLUMN_NAME  from information_schema.table_constraints a  inner join information_schema.constraint_column_usage b  on a.constraint_name = b.constraint_name  where a.constraint_type = 'PRIMARY KEY' and a.table_name = '"
				+ tableName + "'";

		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);

		List list2 = new ArrayList();

		return list;
	}

	public List<Map<String, Object>> getPrimaryKeyssForMSSQL(String tableName, DatabaseConfig config) throws Exception {
		String sql = " select  c.name as COLUMN_NAME from sysindexes i   join sysindexkeys k on i.id = k.id and i.indid = k.indid    join sysobjects o on i.id = o.id    join syscolumns c on i.id=c.id and k.colid = c.colid    where o.xtype = 'U'   and exists(select 1 from sysobjects where  xtype = 'PK'  and name = i.name)     and o.name='"
				+ tableName + "' ";

		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);

		return list;
	}

	public List<Map<String, Object>> getTableColumnsForMSSQL(String tableName, DatabaseConfig config) throws Exception {
		String sql = "select top 1 * from   " + tableName;
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForColumnOnly(sql);

		return list;
	}

	public List<Map<String, Object>> executeSqlForColumnsForMSSQL(String sql, DatabaseConfig config) throws Exception {
		String sql2 = " select top 1 * from (" + sql + ") t  ";
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.executeSqlForColumns(sql2);
		return list;
	}

	public String selectOneColumnTypeForMSSQL(String tableName, String column_name, DatabaseConfig config)
			throws Exception {
		String sql = " select  ISNULL( c.name +'('+  cast(b.length as varchar(10)) +')' , c.name ) as  column_type  from sysobjects a,syscolumns b,systypes c  where a.id=b.id  and a.name='"
				+ tableName + "'  and  b.name='" + column_name + "' and a.xtype='U'  and b.xtype=c.xtype ";

		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);
		return ((String) ((Map) list.get(0)).get("column_type"));
	}

	public boolean copyTableForMySql(String tableName, DatabaseConfig config) throws Exception {
		String sql4 = "create table " + config.databaseName + "." + tableName + "_"
				+ DatetimeUtil.getDefaultCurrentDateTime() + "  select * from " + config.databaseName + "." + tableName;
		DBUtil2 db = DBUtil2.getInstance(config);
		db.setupdateData(sql4);
		return true;
	}

	public boolean copyTableForOracle(String tableName, DatabaseConfig config) throws Exception {
		String sql4 = "create table " + tableName + "_" + DatetimeUtil.getDefaultCurrentDateTime()
				+ " as select * from " + tableName;
		DBUtil2 db = DBUtil2.getInstance(config);
		db.setupdateData(sql4);
		return true;
	}

	public boolean copyTableForPostgreSQL(String tableName, DatabaseConfig config) throws Exception {
		String sql4 = "create table " + tableName + "_" + DatetimeUtil.getDefaultCurrentDateTime()
				+ " as select * from " + tableName;
		DBUtil2 db = DBUtil2.getInstance(config);
		db.setupdateData(sql4);
		return true;
	}

	public boolean renameTableForMySql(String tableName, DatabaseConfig config, String newTableName) throws Exception {
		String sql4 = " rename table " + tableName + " TO  " + newTableName;
		DBUtil2 db = DBUtil2.getInstance(config);
		db.setupdateData(sql4);
		return true;
	}

	public boolean renameTableForOracle(String tableName, DatabaseConfig config, String newTableName) throws Exception {
		String sql4 = " alter table " + tableName + " rename to  " + newTableName;
		DBUtil2 db = DBUtil2.getInstance(config);
		db.setupdateData(sql4);
		return true;
	}

	public boolean renameTableForPostgreSQL(String tableName, DatabaseConfig config, String newTableName)
			throws Exception {
		String sql4 = " alter table " + tableName + " rename to  " + newTableName;
		DBUtil2 db = DBUtil2.getInstance(config);
		db.setupdateData(sql4);
		return true;
	}

	public List<Map<String, Object>> exportDataToSQLForMySQL(String tableName, List<String> condition,
			DatabaseConfig config) throws Exception {
		String sql = " select * from  `" + tableName + "` where   1=1  ";
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = new ArrayList();

		for (int i = 0; i < condition.size(); ++i) {
			List list2 = db.queryForList(sql + ((String) condition.get(i)));
			list.add((Map) list2.get(0));
		}
		return list;
	}

	public List<Map<String, Object>> exportDataToSQLForOracle(String tableName, List<String> condition,
			DatabaseConfig config) throws Exception {
		String sql = " select * from  " + tableName + " where   1=1  ";
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = new ArrayList();

		for (int i = 0; i < condition.size(); ++i) {
			List list2 = db.queryForList(sql + ((String) condition.get(i)));
			list.add((Map) list2.get(0));
		}
		return list;
	}

	public List<Map<String, Object>> exportDataToSQLForPostgreSQL(String tableName, List<String> condition,
			DatabaseConfig config) throws Exception {
		String sql = " select * from  " + tableName + " where   1=1  ";
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = new ArrayList();

		for (int i = 0; i < condition.size(); ++i) {
			List list2 = db.queryForList(sql + ((String) condition.get(i)));
			list.add((Map) list2.get(0));
		}
		return list;
	}

	public List<Map<String, Object>> exportDataToSQLForMSSQL(String tableName, List<String> condition,
			DatabaseConfig config) throws Exception {
		String sql = " select * from  " + tableName + " where   1=1  ";
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = new ArrayList();
		for (int i = 0; i < condition.size(); ++i) {
			List list2 = db.queryForList(sql + ((String) condition.get(i)));
			list.add((Map) list2.get(0));
		}
		return list;
	}

	public boolean deleteBackupFile(String[] ids, String path) throws Exception {
		for (int i = 0; i < ids.length; ++i) {
			File f = new File(path + ids[i]);

			if (f.exists()) {
				f.delete();
			}
		}
		return true;
	}

	public boolean dropTable(String tableName, DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql = " drop  table " + config.databaseName + "." + tableName;
		db.setupdateData(sql);
		return true;
	}

	public boolean dropDatabase(DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql = " drop  database " + config.databaseName;
		db.setupdateData(sql);
		return true;
	}

	public boolean clearTable(String tableName, DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql = " delete from  " + tableName;
		db.setupdateData(sql);
		return true;
	}

	public boolean restoreDBFromFile( String fpath, DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		StringBuffer sb = new StringBuffer();
		BufferedReader reader = null;
		boolean isZS = false;
		boolean isNull = false;

		List sqlList = new ArrayList();
		try {
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(fpath), "UTF-8"));
			String tempString = null;

			while ((tempString = reader.readLine()) != null) {
				tempString = tempString.trim();
				if (tempString.equals("")) {
					isNull = true;
				} else {
					isNull = false;
				}

				if (tempString.indexOf("/*") == 0) {
					isZS = true;
				} else if (tempString.indexOf("*/") >= 0) {
					isZS = false;
				} else {
					if (isZS) {
						continue;
					}

					if (tempString.indexOf("--") == 0) {
						continue;
					}
					if (tempString.indexOf("#") == 0) {
						continue;
					}

					if ((!(isNull)) && (tempString.indexOf("INSERT INTO") != 0)
							&& (tempString.lastIndexOf(";") != tempString.length() - 1)) {
						sb = sb.append(" " + tempString);
					}

					if ((!(isNull)) && (tempString.indexOf("INSERT INTO") != 0)
							&& (tempString.lastIndexOf(";") == tempString.length() - 1)) {
						sb = sb.append(" " + tempString);

						db.setupdateData(sb.toString());
						sb.setLength(0);
					}

					if ((!(isNull)) && (tempString.indexOf("INSERT INTO") == 0)
							&& (tempString.lastIndexOf(";") == tempString.length() - 1)) {
						sqlList.add(tempString);

						if (sqlList.size() >= 3000) {
							db.updateExecuteBatch(sqlList);
							sqlList.clear();
						}

					}

					if (sqlList.size() <= 0) {
						continue;
					}

					db.updateExecuteBatch(sqlList);
					sqlList.clear();
				}
			}

			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			db.releaseConn();
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException localIOException2) {
				}
			}
		}
		return true;
	}
	
	
	public void runScript(List<String> fpath, DatabaseConfig config) {
		ScriptRunner runner=null;
		try {
			DBUtil2 db = DBUtil2.getInstance(config);
			runner = new ScriptRunner(db.getConnection());
			runner.setStopOnError(true);
			// 执行输出
			runner.setLogWriter(new PrintWriter(System.out));
			for (String path : fpath) {
				File file=new File(path);
				InputStreamReader	reader = new InputStreamReader(new FileInputStream(file),"UTF-8");
				runner.runScript(reader);
				if (reader != null) {
					reader.close();
				}
			}
			runner.closeConnection();
		} catch (IOException e) {
		     throw new  BusinessException("无法找到文件:"+e.getMessage());
		}catch(Exception e) {
			throw new  BusinessException("执行脚本出错:"+e.getMessage());
		} 
		finally {
			if(runner!=null) {
				runner.closeConnection();
			}
		}

	}

	public List<Map<String, Object>> viewTableMessForMySql(String tableName, DatabaseConfig config) throws Exception {
		String sql = "   select  *   from information_schema.tables where   table_name='" + tableName
				+ "' and table_schema='" + config.databaseName + "'  ";

		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);

		return list;
	}

	public List<Map<String, Object>> viewTableMessForMSSQL(String tableName, DatabaseConfig config) throws Exception {
		String sql = "  select top 1 t1.name as table_name, t1.crdate ,t1.refdate , t2.*  from   sysobjects t1 left join  sysindexes t2  on   t1.id=t2.id  where  t1.name ='"
				+ tableName + "'  ";

		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);

		return list;
	}

	public List<Map<String, Object>> viewTableMessForPostgreSQL(String tableName, DatabaseConfig config)
			throws Exception {
		String sql = " select * from pg_tables  where tablename='" + tableName + "'  ";
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);

		return list;
	}

	public List<Map<String, Object>> viewTableMessForOracle(String tableName, DatabaseConfig config) throws Exception {
		String sql = "  select *  from user_tables   where table_name ='" + tableName + "' ";
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);

		return list;
	}

	public String getTableRows(String tableName, DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);

		String sql = " select count(*) as num from  " + tableName;
		List list = db.queryForList(sql);
		Map map = (Map) list.get(0);
		String str = String.valueOf(map.get("num"));
		return str;
	}

	public int saveNewTable(JSONArray insertArray, String tableName, DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);

		String sql = " create table " + tableName + " (  ";
		String PRIMARY_KEY = "";

		for (int i = 0; i < insertArray.size(); ++i) {
			Map map1 = (Map) insertArray.get(i);

			sql = sql + map1.get("COLUMN_NAME") + " ";
			sql = sql + map1.get("DATA_TYPE") + " ";

			if (!(map1.get("CHARACTER_MAXIMUM_LENGTH").equals(""))) {
				sql = sql + "(" + map1.get("CHARACTER_MAXIMUM_LENGTH") + ") ";
			}

			if (map1.get("IS_NULLABLE").equals("")) {
				sql = sql + " NOT NULL  ";
			}
			if (!(map1.get("COLUMN_COMMENT").equals(""))) {
				sql = sql + " COMMENT '" + map1.get("COLUMN_COMMENT") + "' ";
			}

			if (map1.get("COLUMN_KEY").equals("PRI")) {
				PRIMARY_KEY = PRIMARY_KEY + map1.get("COLUMN_NAME") + ",";
			}

			if (i < insertArray.size() - 1) {
				sql = sql + " ,";
			}
		}

		if (!(PRIMARY_KEY.equals("")))
			sql = sql + ", PRIMARY KEY (" + PRIMARY_KEY.substring(0, PRIMARY_KEY.length() - 1) + ") ) ";
		else {
			sql = sql + " ) ";
		}

		int y = 0;
		y = db.setupdateData(sql);
		return y;
	}

	public int saveNewTableForMSSQL(JSONArray insertArray, String tableName, DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);

		String sql = " create table " + tableName + " (  ";
		String PRIMARY_KEY = "";
		List commentStr = new ArrayList();

		for (int i = 0; i < insertArray.size(); ++i) {
			Map map1 = (Map) insertArray.get(i);

			sql = sql + map1.get("COLUMN_NAME") + " ";
			sql = sql + map1.get("DATA_TYPE") + " ";
			if (!(map1.get("CHARACTER_MAXIMUM_LENGTH").equals(""))) {
				sql = sql + "(" + map1.get("CHARACTER_MAXIMUM_LENGTH") + ") ";
			}

			if (map1.get("IS_NULLABLE").equals("")) {
				sql = sql + " NOT NULL  ";
			}

			if (map1.get("COLUMN_KEY").equals("PRI")) {
				PRIMARY_KEY = PRIMARY_KEY + map1.get("COLUMN_NAME") + ",";
			}

			if (i < insertArray.size() - 1) {
				sql = sql + " ,";
			}
		}

		if (!(PRIMARY_KEY.equals("")))
			sql = sql + ", PRIMARY KEY (" + PRIMARY_KEY.substring(0, PRIMARY_KEY.length() - 1) + ") ) ";
		else {
			sql = sql + " ) ";
		}

		int y = 0;
		y = db.setupdateData(sql);

		return y;
	}

	public int saveNewTableForOracle(JSONArray insertArray, String tableName, DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);

		String sql = " create table " + tableName + " (  ";
		String PRIMARY_KEY = "";
		List commentStr = new ArrayList();
		for (int i = 0; i < insertArray.size(); ++i) {
			Map map1 = (Map) insertArray.get(i);

			sql = sql + map1.get("COLUMN_NAME") + " ";
			sql = sql + map1.get("DATA_TYPE") + " ";
			if (!(map1.get("CHARACTER_MAXIMUM_LENGTH").equals(""))) {
				sql = sql + "(" + map1.get("CHARACTER_MAXIMUM_LENGTH") + ") ";
			}

			if (map1.get("IS_NULLABLE").equals("")) {
				sql = sql + " NOT NULL  ";
			}

			if (!(map1.get("COLUMN_COMMENT").equals(""))) {
				commentStr.add(" COMMENT ON COLUMN  " + tableName + "." + map1.get("COLUMN_NAME") + " IS  '"
						+ map1.get("COLUMN_COMMENT") + "'");
			}

			if (map1.get("COLUMN_KEY").equals("PRI")) {
				PRIMARY_KEY = PRIMARY_KEY + map1.get("COLUMN_NAME") + ",";
			}

			if (i < insertArray.size() - 1) {
				sql = sql + " ,";
			}
		}

		if (!(PRIMARY_KEY.equals("")))
			sql = sql + ", PRIMARY KEY (" + PRIMARY_KEY.substring(0, PRIMARY_KEY.length() - 1) + ") ) ";
		else {
			sql = sql + " ) ";
		}

		int y = 0;
		y = db.setupdateData(sql);

		for (int i = 0; i < commentStr.size(); ++i) {
			db.setupdateData((String) commentStr.get(i));
		}
		return y;
	}

	public Map<String, Object> queryDatabaseStatus(DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql = " show global status  ";

		List list = db.queryForList(sql);
		Map map = new HashMap();

		String Variable_name = "";
		String Value = "";
		for (int i = 0; i < list.size(); ++i) {
			Map temp = (Map) list.get(i);
			Variable_name = (String) temp.get("Variable_name");
			Value = (String) temp.get("Value");
			map.put(Variable_name, Value);
		}
		return map;
	}

	public Map<String, Object> queryDatabaseStatusForOracle( DatabaseConfig config)
			throws Exception {
		Map map = new HashMap();
		String Variable_name = "";
		String Value = "";

		map.put("SESSIONS", queryDatabaseStatusForOracleSession(config));
		map.put("HIT_RATIO", queryDatabaseStatusForOracleHitRatio(config));
		map.put("HIT_RADIO", queryDatabaseStatusForOracleHitRadio(config));
		map.put("LOG_BUFFER", queryDatabaseStatusForOracleLogBuffer(config));
		map.put("LOCK", queryDatabaseStatusForOracleLock(config));
		map.put("PHYRDS", queryDatabaseStatusForOraclePhyrds(config));
		map.put("PHYWRTS", queryDatabaseStatusForOraclePhywrts(config));

		return map;
	}

	public String queryDatabaseStatusForOracleSession(DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql = "select count(*) as SESSIONS from  v$session";
		List list = db.queryForList(sql);
		String value = "0";
		if (list.size() > 0) {
			Map temp = (Map) list.get(0);
			value = (String) temp.get("SESSIONS");
		}
		return value;
	}

	public String queryDatabaseStatusForOracleHitRatio(DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql = "select floor(( 1 - sum(decode(name, 'physical reads', value, 0)) /(sum(decode(name, 'db block gets', value, 0)) + sum(decode(name, 'consistent gets', value, 0)))) *100)  HIT_RATIO from v$sysstat t where name in ('physical reads', 'db block gets', 'consistent gets')";
		List list = db.queryForList(sql);
		String value = "0";
		if (list.size() > 0) {
			Map temp = (Map) list.get(0);
			value = (String) temp.get("HIT_RATIO");
		}
		return value;
	}

	public String queryDatabaseStatusForOracleHitRadio(DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql = "select  floor(sum(pinhits)/sum(pins)*100 ) AS HIT_RADIO from v$librarycache";
		List list = db.queryForList(sql);
		String value = "0";
		if (list.size() > 0) {
			Map temp = (Map) list.get(0);
			value = (String) temp.get("HIT_RADIO");
		}
		return value;
	}

	public String queryDatabaseStatusForOracleLogBuffer(DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql = "select floor((select value  from v$sysstat where name in( 'redo buffer allocation retries'))/ (select value  from v$sysstat where name in('redo entries' ) )) as LOG_BUFFER from dual";
		List list = db.queryForList(sql);
		String value = "0";
		if (list.size() > 0) {
			Map temp = (Map) list.get(0);
			value = (String) temp.get("LOG_BUFFER");
		}
		return value;
	}

	public String queryDatabaseStatusForOracleLock(DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql = "select count(*) as LOCKS from v$locked_object ";
		List list = db.queryForList(sql);
		String value = "0";
		if (list.size() > 0) {
			Map temp = (Map) list.get(0);
			value = (String) temp.get("LOCKS");
		}
		return value;
	}

	public String queryDatabaseStatusForOraclePhyrds(DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql = "select sum(f.phyrds) PHYRDS from v$filestat f, dba_data_files df where f.file# = df.file_id ";
		List list = db.queryForList(sql);
		String value = "0";
		if (list.size() > 0) {
			Map temp = (Map) list.get(0);
			value = (String) temp.get("PHYRDS");
		}
		return value;
	}

	public String queryDatabaseStatusForOraclePhywrts(DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql = "select sum(f.phywrts) PHYWRTS from v$filestat f, dba_data_files df where f.file# = df.file_id ";
		List list = db.queryForList(sql);
		String value = "0";
		if (list.size() > 0) {
			Map temp = (Map) list.get(0);
			value = (String) temp.get("PHYWRTS");
		}
		return value;
	}

	public List<Map<String, Object>> queryTableSpaceForOracle(DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql = " SELECT a.tablespace_name TABLESPACE_NAME ,ROUND( free / (1024 * 1024 ),2) TABLESPACE_SIZE_FREE,ROUND( total / (1024 * 1024 ),2) TABLESPACE_SIZE,   ROUND( (total - free) / (1024 * 1024  ),2) TABLESPACE_SIZE_USED FROM (SELECT tablespace_name, SUM(bytes) free FROM dba_free_space GROUP BY tablespace_name) a, (SELECT tablespace_name, SUM(bytes) total FROM dba_data_files GROUP BY tablespace_name) b WHERE a.tablespace_name = b.tablespace_name  ";

		List list = db.queryForList(sql);
		return list;
	}

	public List<Map<String, Object>> monitorItemValue( DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql = " show global status  ";
		List list2 = new ArrayList();
		List list = db.queryForList(sql);

		String Variable_name = "";
		String Value = "";
		for (int i = 0; i < list.size(); ++i) {
			Map tempMap = (Map) list.get(i);
			Variable_name = (String) tempMap.get("Variable_name");
			Value = (String) tempMap.get("Value");

			if (Variable_name.equals("Com_select")) {
				tempMap.put("descript", "select数量");
			} else if (Variable_name.equals("Com_update")) {
				tempMap.put("descript", "更新数量");
			} else if (Variable_name.equals("Open_tables")) {
				tempMap.put("descript", "当前打开的表的数量");
			} else if (Variable_name.equals("Open_files")) {
				tempMap.put("descript", "打开的文件的数目");
			} else if (Variable_name.equals("Opened_tables")) {
				tempMap.put("descript", "已经打开的表的数量");
			} else if (Variable_name.equals("Questions"))
				tempMap.put("descript", "已经发送给服务器的查询的个数");
			else {
				tempMap.put("descript", "");
			}
			list2.add(tempMap);
		}

		return list2;
	}

	public List<Map<String, Object>> monitorItemValueForOracle(DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		String sql = "  select NAME \"Variable_name\", VALUE \"Value\"  from v$sysstat ";
		List list2 = new ArrayList();
		List list = db.queryForList(sql);
		String Variable_name = "";
		String Value = "";
		for (int i = 0; i < list.size(); ++i) {
			Map tempMap = (Map) list.get(i);
			Variable_name = (String) tempMap.get("Variable_name");
			Value = (String) tempMap.get("Value");

			list2.add(tempMap);
		}

		return list2;
	}

	public List<Map<String, Object>> selectAllDataFromSQLForOracle( DatabaseConfig config,
			String sql) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);
		return list;
	}

	public List<Map<String, Object>> selectAllDataFromSQLForPostgreSQL( DatabaseConfig config,
			String sql) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);
		return list;
	}

	public List<Map<String, Object>> selectAllDataFromSQLForMSSQL( DatabaseConfig config,
			String sql) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);
		List list = db.queryForList(sql);
		return list;
	}

	public boolean insertByDataListForMySQL(List<Map<String, Object>> dataList,  String tableName,
			DatabaseConfig config) throws Exception {
		DBUtil2 db = DBUtil2.getInstance(config);

		int y = 0;
		List sqlList = new ArrayList();
		String colums = " ";
		String values = " ";
		String tempValues = "";
		String insertSQL = "";

		for (Map<String, Object> map4 : dataList) {
			insertSQL = "INSERT INTO `" + tableName + "` ";
			colums = " ";
			values = "";
			for (Map.Entry entry : map4.entrySet()) {
				colums = colums + ((String) entry.getKey()) + ",";

				if (entry.getValue() == null) {
					values = values + "null,";
				} else if ((entry.getValue() instanceof java.sql.Date) || (entry.getValue() instanceof Time)
						|| (entry.getValue() instanceof Timestamp)) {
					values = values + "'" + entry.getValue() + "',";
				} else if ((entry.getValue() instanceof Integer) || (entry.getValue() instanceof Float)
						|| (entry.getValue() instanceof Long) || (entry.getValue() instanceof BigInteger)
						|| (entry.getValue() instanceof Double) || (entry.getValue() instanceof BigDecimal)) {
					values = values + entry.getValue() + ",";
				} else if (entry.getValue() instanceof Byte) {
					byte[] ss = (byte[]) entry.getValue();

					if (ss.length == 0)
						values = values + "null,";
					else {
						values = values + "0x" + bytesToHexString(ss) + ",";
					}
				} else if (entry.getValue() instanceof ArrayList) {
					values = values + "'" + entry.getValue().toString() + "',";
				} else {
					tempValues = (String) entry.getValue();

					tempValues = tempValues.replace("'", "\\'");

					tempValues = tempValues.replace("\r\n", "\\r\\n");
					tempValues = tempValues.replace("\n\r", "\\n\\r");
					tempValues = tempValues.replace("\r", "\\r");
					tempValues = tempValues.replace("\n", "\\n");

					values = values + "'" + tempValues + "',";
				}
			}

			colums = colums.substring(0, colums.length() - 1);
			values = values.substring(0, values.length() - 1);

			insertSQL = insertSQL + " ( " + colums + ") VALUES (" + values + " ) ";
			sqlList.add(insertSQL);
		}

		y = db.updateExecuteBatch(sqlList);
		return true;
	}

	public String bytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder("");
		if ((src == null) || (src.length <= 0)) {
			return null;
		}
		for (int i = 0; i < src.length; ++i) {
			int v = src[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}

	public int executeQueryForCount(String sql, DatabaseConfig config) {
		DBUtil2 db = DBUtil2.getInstance(config);
		int rowCount = db.executeQueryForCount("select count(*) from (" + sql + ") as temp1 ");
		return rowCount;
	}

}
